<?php

namespace app\admin\controller;

use app\admin\model\Msg as ModelMsg;
use app\admin\model\User;
use app\BaseController;
use PDO;
use think\facade\Session;
use think\facade\View;

class Msg extends Base
{
    public function send()
    {
        $userList = User::column('username,id');
        return view('send', ['userList' => $userList]);
    }
    public function doSend()
    {
        if (request()->isPost()) {
            $param = input();
            if (empty(@$param['msg']) && empty(@$param['user_id'])) return error('缺少必要参数');
            $hasUser = User::where('id', $param['user_id']);
            $msg = $param['msg'];
            if ($param['user_id'] != 0) {
                if ($hasUser->findOrEmpty()->isEmpty()) return error('用户不存在');
                $receriver_uid = $hasUser->find()->id;
                ModelMsg::send(0, $receriver_uid, ['content' => $msg]);
            } else {
                $listUser = User::select();
                foreach ($listUser as $k => $v) {
                    ModelMsg::send(0, $v->id,['content' => $msg]);
                }
            }
            return success();
        }
    }
}
